<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-tabs">
        <h2>欢迎使用本系统</h2>
        <el-form ref="loginForm" :model="loginForm" :rules="rules" class="login-form">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item>
            <div class="login-button">
              <el-button type="primary" size="large" @click="handleLogin">登录</el-button>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loginForm: {
        username: "root",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "密码长度在6到20个字符之间",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    async handleLogin() {
      // 处理账号登录
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          // 验证码验证通过，跳转到下一个页面
          // 在这里提交表单
          await this.$axios
            .post("/api/emp/login", this.loginForm)
            .then((res) => {
              if (res.data.code === 1) {
                localStorage.setItem("empToken", res.data.data);
                this.$message.success(res.data.msg);
                this.$router.replace("/manageMain");
              } else if (res.data.code === 0) {
                this.$message.error(res.data.msg);
              }
            })
            .catch((error) => {
              // 处理请求失败的错误
              if (!error.response) {
                this.$message.error(error.message);
              }
              this.$message.error(error.response.data.msg);
            });
        } else {
          this.$message.error("请正确填写信息");
          return false;
        }
      });
    },
  },
  mounted() {},
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
  background-size: cover;
  background-position: center;
}

.login-box {
  width: 400px;
  height: auto;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  padding: 20px;
}

.login-form {
  margin-top: 20px;
}

.get-code-btn {
  float: right;
  margin-top: 5px;
}

.login-button {
  display: flex;
  flex-direction: column;
  /* align-items: center; */
}
</style>